package com.atomic.test.example.service.impl;

import com.atomic.test.example.entity.Board;
import com.atomic.test.example.entity.BoardManager;
import com.atomic.test.example.exception.CRUDException;
import com.atomic.test.example.exception.ExceptionCode;
import com.atomic.test.example.query.BaseQuery;
import com.atomic.test.example.query.PageList;
import com.atomic.test.example.repository.BoardManagerRepository;
import com.atomic.test.example.repository.BoardRepository;
import com.atomic.test.example.service.IBoardService;
import com.atomic.test.example.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by dreamyao on 2017/1/27.
 */
@Service
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardRepository boardRepository;
    @Autowired
    private BoardManagerRepository boardManagerRepository;

    /**
     * 创建论坛
     *
     * @param board
     */
    @Override
    public Integer createBoard(Board board) {
        String boardName = board.getBoardName();
        Board queryBoard = this.getBoardMassage(boardName);
        if (queryBoard != null) {
            throw new CRUDException(ExceptionCode.HAVE_REPEAT_DATA, "有相同的论坛板块名存在!");
        }
        boardRepository.save(board);
        BoardManager boardManager = new BoardManager();
        boardManager.setBoardId(board.getId());
        boardManager.setUserId(UserContext.getLoginUser().getId());
        boardManagerRepository.save(boardManager);
        return board.getId();
    }

    /**
     * 根据论坛板块名获取论坛信息
     *
     * @param boardName
     * @return
     */
    @Override
    public Board getBoardMassage(String boardName) {
        return boardRepository.getBoardByBoardName(boardName);
    }

    /**
     * 根据论坛板块ID获取论坛信息
     *
     * @param id
     * @return
     */
    @Override
    public Board getMessageById(Integer id) throws CRUDException {
        return boardRepository.getById(id);
    }

    /**
     * 根据论坛板块Id删除论坛板块
     *
     * @param boardId
     */
    @Override
    public void deleteBoard(Integer boardId) {
        Board queryBoard = boardRepository.getById(boardId);
        boardRepository.deleteBoardById(boardId);
    }

    /**
     * 获取所有论坛板块信息
     *
     * @return
     */
    @Override
    public List<Board> findAllBoard() {
        return boardRepository.findAll();
    }

    /**
     * 根据板块Id更新话题数
     *
     * @param boardId
     * @param topicNum
     */
    @Override
    public Integer updateTopicNumByBoardId(Integer boardId, Integer topicNum) {
        Integer num = boardRepository.updateTopicNumByBoardId(boardId, topicNum);
        if (num == 0) {
            throw new CRUDException(ExceptionCode.DATA_INSERT_ERROR, "更新话题数失败！");
        }
        return num;
    }

    /**
     * 保存
     *
     * @param board
     */
    @Override
    public void save(Board board) {

    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {

    }

    /**
     * 更新论坛板块
     *
     * @param board
     */
    @Override
    public void update(Board board) {
        Integer id = board.getId();
        try {
            Board queryBoard = this.getMessageById(id);
            queryBoard.setBoardName(board.getBoardName());
            queryBoard.setBoardDesc(board.getBoardDesc());
            boardRepository.save(queryBoard);
        } catch (CRUDException e) {
            e.printStackTrace();
            throw new CRUDException(ExceptionCode.HAVE_NOT_DATA, "论坛板块ID不存在，更新失败!");
        }
    }

    /**
     * 根据id来查找
     *
     * @param id
     * @return
     */
    @Override
    public Board get(Integer id) {
        return null;
    }

    @Override
    public PageList findPage(BaseQuery baseQuery) {
        return null;
    }

    /**
     * 得到所有
     *
     * @return
     */
    @Override
    public List<Board> getAll() {
        return null;
    }

}
